import Vue from "vue";
import {METHOD, request} from "./request";

export default {
    install: function () {
        Vue.prototype.$get = function (url, params = {}) {
            var t = this;
            return new Promise(((resolve, reject) => {
                request(url, METHOD.GET, params)
                    .then(res => {
                        var r = res.data
                        if (!r.success) {
                            t.$message.error(r.errMessage)
                            reject()
                        } else {
                            resolve(r.data)
                        }
                    })
                    .catch(e => reject(e))
            }))
        }

        Vue.prototype.$post = function (url, data) {
            var t = this;
            return new Promise(function (resolve, reject) {
                request(url, METHOD.POST, data)
                    .then(res => {
                        var r = res.data
                        if (!r.success) {
                            t.$message.error(r.errMessage)
                            reject()
                        } else {
                            resolve(r.data)
                        }
                    })
            }).catch(e => {
                reject(e)
            })
        }


        Vue.prototype.$clone = function (obj) {
            return JSON.parse(JSON.stringify(obj))
        }
    }
}
